import { getLogger } from 'log4js';
import { QoS } from 'mqtt';
import connect from './connect';

const logger = getLogger();

/**
 * 发送消息
 * @param uri mqtt连接地址
 * @param topic 主题
 * @param msg 消息
 * @param qos Subscribe的级别
 */
export default async function semd(uri: string, topic: string, msg: string, qos: QoS) {
	const client = await connect(uri);

	client.publish(topic, msg, { qos }, (err) => {
		if (err) {
			logger.error(err);
		}
		client.end();
	});
}
